from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re


from selenium.webdriver.common.keys import Keys

class yandexru:
	
    def addPost(self, proxy, account, post):
        try:
            if(proxy == True):

                PROXY_HOST = "127.0.0.1"
                PROXY_PORT = 8118

                fp = webdriver.FirefoxProfile()

                # Direct = 0, Manual = 1, PAC = 2, AUTODETECT = 4, SYSTEM = 5
                fp.set_preference("network.proxy.type", 1)

                fp.set_preference("network.proxy.http", PROXY_HOST)
                fp.set_preference("network.proxy.http_port", PROXY_PORT)
                fp.set_preference("network.proxy.ftp", PROXY_HOST)
                fp.set_preference("network.proxy.ftp_port", PROXY_PORT)
                fp.set_preference("network.proxy.ssl", PROXY_HOST)
                fp.set_preference("network.proxy.ssl_port", PROXY_PORT)

                fp.set_preference("network.proxy.no_proxies_on", "") # set this value as desired

                #driver = webdriver.Firefox(firefox_profile=fp)



                driver = webdriver.Firefox(firefox_profile=fp) # Get local session of firefox
            else:
                driver = webdriver.Firefox()

	        driver.implicitly_wait(30)
            base_url = "http://pda-passport.yandex.ru/passport?mode=login&retpath=http%3A%2F%2Fmy.yandex.ru%2F"
            driver.get(base_url)
            driver.find_element_by_id("b-domik__username").clear()
            driver.find_element_by_id("b-domik__username").send_keys(account[0])
            driver.find_element_by_id("b-domik__password").clear()
            driver.find_element_by_id("b-domik__password").send_keys(account[1])
            driver.find_element_by_css_selector("input.b-domik__submit").click()

            time.sleep(5)
            driver.get("http://my.yandex.ru/"+account[0]+"/posts_add_link.xml")
            time.sleep(5)
            driver.find_element_by_id("url-input").clear()
            driver.find_element_by_id("url-input").send_keys(post[0])
            driver.find_element_by_id("title-input").clear()
            driver.find_element_by_id("title-input").send_keys(post[1])
            driver.find_element_by_name("body").clear()
            driver.find_element_by_name("body").send_keys(post[2])
            driver.find_element_by_name("tags").clear()
            driver.find_element_by_name("tags").send_keys(post[3])
            driver.find_element_by_css_selector("input.b-add-post__btn").click()
            time.sleep(8)
            driver.close()
            return True
        
        except :
            
            driver.close()
            return False


